$(document).ready(function(){
	var token=localStorage.getItem("token");
	var username=localStorage.getItem("username");
	var id = tl_config.his_url;
	id = id.substring(id.indexOf("id=")+3, id.length);
	id = id.substring(0, id.indexOf("&"));
	var source=tl_config.his_url.split("&")[1].split("=")[1];
	if("list"==source){
		$("div .z_title").html('首页&gt;员工管理&gt;管辖区域');
	}
	if("assignPost"==source){
		$("div .z_title").html('首页&gt;员工管理&gt;编辑员工&gt;分配岗位&gt;管辖区域');
	}
	$.blockUI({message:'<br/><img src='+dialogclosepath+'/images/loading.gif'+' align="absmiddle" />&nbsp;&nbsp;初始化，请稍候...<br/><br/>'});
	
	//原来的区域
	var oldAreas=[];
	//原来的区域name
	var oldAreaNames=[];
	$.ajax({
		url:server_url+"rest/assignPrecinct.do?ran="+Math.random(),
		type:"GET",
		data:{
			staffId:id,
			token:token
		},
		success:function(data){
			if(data.retCode='0000'){
				var settings={
					keyName:"areaId",
					parentKeyName:"parentId",
					childKeyName:"nodes"
				};
				var area=transformTozTreeFormat(settings,data.data);
				
				$.fn.tree.init({nodes:area});
				//储存原来的区域name
				getArea(oldAreas);
				for(var i in oldAreas){
					oldAreaNames.push(oldAreas[i].name);
				}
				$.unblockUI();
			}else {
				easyDialog.open({
					container:{
						header:'温馨提示',
						content:data.retMsg
					},
					autoClose : 2000
				});					
			}
		}
	});	
	
	//点击全国
	$(".z_address1 input").live('click',function(){
		$.blockUI();
		var isChecked;
		if(true==$(this).prop("checked")){
			isChecked="true";
		}else{
			isChecked="false";
		}
		$.ajax({
			 type:"get",
			 url:server_url+"sysAreaManager/get4LevelAreas.do?ran="+Math.random(),
		  	 data:{
		  		 isChecked:isChecked,
		  		 token:token
		  	 },
		  	 success:function(data){
		  		if(data.retCode='0000'){
					var settings={
						keyName:"areaId",
						parentKeyName:"parentId",
						childKeyName:"nodes"
					};
					var area=transformTozTreeFormat(settings,data.data);
					$.fn.tree.init({nodes:area});
					$.unblockUI();
				}else {
					$.unblockUI();
					easyDialog.open({
						container:{
							header:'温馨提示',
							content:data.retMsg
						},
						autoClose : 2000
					});					
				}
		  	 }
		  });
	});
	
	
	
	$(".z_sure").click(function(){
		$.blockUI();
		var areaIdArr=new Array();
		//新区域
		var newAreas=[];
		var newAreaNames=[];
		//新勾选区域树的根节点
		var rootNodes=[];
		$('input:checkbox:checked').each(function(i,v){
			areaIdArr.push($.parseJSON($(v).val()).areaId);
		});
		//储存新的区域name和id
		getArea(newAreas);
		for(var i in newAreas){
			newAreaNames.push(newAreas[i].name);
		}
		for(var i in newAreas){
			rootNodes.push(newAreas[i].id);
		}
		$.ajax({
			type:"post",
			url:server_url+"rest/savePrecinct.do?ran="+Math.random(),
			traditional:true,
			data:{
				staffId:id,
				userName:username,
				areaIdArr:areaIdArr,
				oldAreas:oldAreaNames,
				newAreas:newAreaNames,
				rootNodes:rootNodes,
				token:token
			},
			success:function(data){
				$.unblockUI();
				if(data.retCode='0000'){
					var url='staffList.do?ran='+Math.random();
					tl_config.loadToWorkArea(url);
				}else {
					easyDialog.open({
						container:{
							header:'温馨提示',
							content:data.retMsg
						},
						autoClose : 2000
					});					
				}
			}
		});
	});
	
	$(".z_goback").click(function(){
		if("list"==source){
			var url='staffList.do?id='+id+'&ran='+Math.random();
			tl_config.loadToWorkArea(url);
		}
		if("assignPost"==source){
			var url='assignPost.do?id='+id+'&source=edit&ran='+Math.random();
			tl_config.loadToWorkArea(url);
		}
	});
	
	//日志记录，筛选区域
	function getArea(arr){
		var nation=$(".nation input");
		//全国
		if(nation[0].checked==true){
			var tmp={};
			var jsonObj=JSON.parse($(nation[0]).val());
			tmp.id=jsonObj.areaId;
			tmp.name=jsonObj.areaName;
			arr.push(tmp);
			return;
		}
		var province=$(".z_dizhi2 input");
		//省
		for(var i=0;i<province.length;++i){
			if(province[i].checked==false){
				//市
				var city=$(province[i]).parent().parent().siblings(".z_address3").find(".city input");
				for(var j=0;j<city.length;++j){
					if(city[j].checked==false){
						//县
						var county=$(city[j]).parent().parent().siblings(".z_address4").find("input");
						for(var k=0;k<county.length;++k){
							if(county[k].checked==true){
								tmp={};
								jsonObj=JSON.parse($(county[k]).val());
								tmp.id=jsonObj.areaId;
								tmp.name=jsonObj.areaName;
								arr.push(tmp);
							}
						}
					}else{
						tmp={};
						jsonObj=JSON.parse($(city[j]).val());
						tmp.id=jsonObj.areaId;
						tmp.name=jsonObj.areaName;
						arr.push(tmp);
					}
				}
			}else{
				tmp={};
				jsonObj=JSON.parse($(province[i]).val());
				tmp.id=jsonObj.areaId;
				tmp.name=jsonObj.areaName;
				arr.push(tmp);
			}
			
		}
		return;
	}
	
});




